//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Reflection;

namespace OpenLS.Drawing
{
    ///<summary>
    /// Provides access to the built-in themes.
    ///</summary>
    public class BuiltInThemes
    {
        private static Theme _defaultTheme;

        [ThreadStatic] private static List<Theme> _instances;

        ///<summary>
        /// Enumerates all the built-in themes.
        ///</summary>
        public static IEnumerable<Theme> Instances
        {
            get
            {
                //\\_instances = null; //\\
                if (_instances == null)
                {
                    _instances = new List<Theme>();
                    using (
                        Stream stream =
                            Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenLS.Drawing.All.thmx"))
                    {
                        using (Package package = Package.Open(stream))
                        {
                            var doc = new ThemeDocument();
                            doc.ReadPackage(package);
                            foreach (Theme theme in doc.Themes)
                                _instances.Add(theme);
                        }
                    }
                }
                return _instances;
            }
        }

        public static IEnumerable<ColorScheme> ColorSchemes
        {
            get
            {
                foreach (Theme theme in Instances)
                {
                    yield return theme.ColorScheme;
                }
            }
        }

        public static IEnumerable<FontScheme> FontSchemes
        {
            get
            {
                foreach (Theme theme in Instances)
                {
                    yield return theme.FontScheme;
                }
            }
        }

        public static Theme DefaultTheme
        {
            get
            {
                if (_defaultTheme == null)
                    _defaultTheme = getTheme("Office");
                return _defaultTheme;
            }
        }

        private static Theme getTheme(string name)
        {
            {
                foreach (Theme theme in Instances)
                    if (theme.Name == name)
                        return theme;
                return null;
            }
        }
    }
}